package com.neusoft.demo.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.neusoft.demo.pojo.Foods;
import com.neusoft.demo.pojo.Yuangong;
import com.neusoft.demo.server.FoodsServer;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@Controller
@RequestMapping("/foods")
public class FoodsController {
    @Autowired
    FoodsServer foodsServer;

    @ResponseBody
    @RequestMapping("/list")
    public java.util.List<Foods> foodsList(int bid){
        
        return foodsServer.foodsList(bid);
    }

    @ResponseBody
    @PostMapping("/add")
    public boolean addFoods(Foods foods){
       return foodsServer.addFoods(foods);


    }
    @ResponseBody
    @RequestMapping("/update")
    public boolean updateFoods(Foods foods){
        return foodsServer.updateFoods(foods);
    }
    @ResponseBody
    @RequestMapping("/upTot")
    public boolean updateTot(int tot,int foodId){

        return foodsServer.upTot(tot, foodId);
    }
    @ResponseBody
    @RequestMapping("/upT")
    public boolean upT(int tot){

        return foodsServer.upT(tot);
    }
    @ResponseBody
    @RequestMapping("/del")
    public  boolean delBumen(int foodId){

        return foodsServer.delFoods(foodId);
    }
    @ResponseBody
    @RequestMapping("/search")
    public java.util.List<Foods> search(String foodName, int beginSal, int endSal,int businessId){
        System.out.println("后台数据>>>>>"+businessId);
        System.out.println("后台数据endSal>>>>>"+endSal);
        return foodsServer.search(foodName,beginSal,endSal,businessId);
    }
    @ResponseBody
    @RequestMapping("/findFoodsById")
    public java.util.List<Foods> findFoodsById(){

        return foodsServer.findYuangongById();
    }
    //处理文件上传
    @ResponseBody
    @RequestMapping("/upload")
    String saveFile(HttpServletRequest request ,MultipartFile file){
        //处理文件名字 避免同名冲突
        String n=file.getOriginalFilename();
        //截取后缀名
        String ext=n.substring(n.lastIndexOf("."));
        //拼接uuid 保证名字统一
        String uploadFile= UUID.randomUUID()+ext;

        //确定文件上传的地址
       // String serverPath= request.getServletContext().getRealPath("/"); 获取tomcat根目录
        String serverPath=null;

        try {
            serverPath= ResourceUtils.getURL("classpath:").getPath();//获得路径
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        File cf=new File(serverPath+"//upload//",uploadFile);//组建最后的路径 在server下创建upload文件夹  uploadFile为存储的名字
        System.out.println("上传路径："+cf.getAbsolutePath());
        //路径不存在 新建路径
//        if(!cf.exists()){
//            cf.mkdirs();
//        }
        if(!cf.exists()){
            cf.mkdirs();
        }
        try {
            file.transferTo(cf);//另存为
            System.out.println("文件上传成功");

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return "http://localhost:8080/"+uploadFile;
    }

}
